#!/usr/bin/env node

const argv = process.argv;
const log = Math.log;
let h = 0,
p = 0,
arr = [];

for(let i=2;i<argv.length; i++){
  let item = -Number(argv[i])*log(Number(argv[i]),2);
  h += item;
  p += Number(argv[i]); 
}
//console.log('p:',p)
//console.log('h:',h)

if(p > 1){
  console.log('概率空间不封闭，概率大于1');
  console.log('请输入信源概率分布：（Ctrl+D 退出输入)');
}
if(p < 1){
  console.log('概率空间不封闭，概率小于1');
  console.log('请输入信源概率分布：（Ctrl+D 退出输入）');
}
if(p === 1){
  for(let i=2; i<argv.length; i++){
    arr = [...arr,argv[i]];
  }
  console.log(`p = [${arr}] `);
  console.log(`h(p) = ${h} bit`)
}
